package cloud.xlh.digital_power_order.util.batch.impl;

import cloud.xlh.digital_power_order.util.batch.FileParesFunctionInterface;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/9/12 13:54
 * @ Description:
 */
@Slf4j
public class FileParesImpl {
    private static final FileParesFunctionInterface  fileParesFunctionInterface= new FileParesFunctionInterface<String , Map>() {
        @Override
        public Map parseFile(String  filePath) {
            //文件读取策略
            //文件解析策略
            List<String> dataList = new ArrayList<>();
            try {
                RandomAccessFile file = new RandomAccessFile(filePath, "r");
                String content;
                while ((content = file.readLine()) != null) {
                    dataList.add(content);
                }
                file.close();
            } catch (IOException e) {
                log.error("文件读取失败，原因：{}，信息：{}", e.getCause() , e.getMessage());
                throw new RuntimeException("文件读取失败");
            }
            Map<String, String> map = new HashMap<>();
            dataList.forEach(new Consumer<String>() {
                @Override
                public void accept(String content) {
                    String[] dataArrayAfterSpl = content.split("|");
                        map.put("流水号" , dataArrayAfterSpl[0]);
                        map.put("交易金额" , dataArrayAfterSpl[1]);
                        map.put("支付状态" , dataArrayAfterSpl[2]);
                }
            });
            return map;
        }
    };

}
